package com.example.zhidingyiview.zhidingyiview;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import com.example.zhidingyiview.R;


public class BallView extends View {

    private final Bitmap mBall;
    private final Paint mPaint;
    private float mLeft;
    private float mTop;
    
    public BallView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        mBall = BitmapFactory.decodeResource(getResources(), R.drawable.ball);
        mPaint = new Paint();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        //手指触摸哪里,小球就画到哪里
        canvas.drawBitmap(mBall,mLeft,mTop,mPaint);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        mLeft = event.getX();
        mTop = event.getY();

        //重新绘制界面
        invalidate();
        //为什么改成true滑动小球就可以跟着手指滑动???
        //而默认情况只有点击才会跟着走
        //明天讲事件分发之后就会明白
        return true;
    }
}
